package com.ysxsoft.wdg.ui.activity.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ali.auth.third.ui.webview.BaseWebViewActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.home.MsgAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.base.BaseGWebViewActivity;
import com.ysxsoft.wdg.bean.home.MessageBean;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class MessageActivity extends BaseActivity implements OnRefreshListener, OnLoadmoreListener {


    public static final void start(Context context) {
        Intent intent = new Intent(context, MessageActivity.class);
        context.startActivity(intent);
    }

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_finish)
    ImageView titleFinish;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;

    private MsgAdapter adapter;
    private int page = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_list;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleContent.setText("消息");

        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new MsgAdapter(R.layout.item_msg);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MessageBean.DataBean  bean= (MessageBean.DataBean) adapter.getItem(position);
                BaseGWebViewActivity.start(MessageActivity.this,"消息详情",bean.getContent());
            }
        });
        recyclerview.setAdapter(adapter);

        getList();
    }

    private void getList() {
        multStatusView.showLoading();
        OkHttpUtils.post()
                .url(UrlHost.SYS_MESSAGE)
                .addParams("uid",  SharePrefUtils.getUserId())
                .addParams("page", page + "")
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                    multStatusView.showError();
                    reset();
                }
            }

            @Override
            public void onResponse(String response, int id) {
                reset();
                if (response != null) {
                    multStatusView.showContent();
                    MessageBean messageBean = JsonUtils.parseByGson(response, MessageBean.class);
                    if (messageBean == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(messageBean.getCode())) {
                        List<MessageBean.DataBean> list = messageBean.getData();
                        if (page == 1) {
                            adapter.setNewData(list);
                        } else {
                            adapter.addData(list);
                        }
                    }else if(CallbackCode.EMPTY.equals(messageBean.getCode())){
                        multStatusView.showEmpty();
                    }
                } else {
                    multStatusView.showError();
                }
            }
        });
    }

    private void reset() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh(true);
        }
        refreshLayout.finishLoadmore();
    }

    @Override
    protected void setListener() {

    }

    @OnClick(R.id.title_finish)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        page++;
        getList();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        page = 1;
        getList();
    }
}
